#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <pthread.h>

//对应笔记：线程1

using namespace std;

int val = 0;
// void* routine(void* args)
// {
//     const char* name = (const char*)args;
//     while(true)
//     {
//         cout << "我是新线程！name: " << name << " val: " << val++ << " &val: " << &val << endl;
//         sleep(1);
//     }
// }

//一个线程出现问题，也会影响其他线程
void* routine(void* args)
{
    const char* name = (const char*)args;
    while(true)
    {
        cout << "我是新线程！name: " << name << " val: " << val++ << " &val: " << &val << endl;
        int* p = nullptr;
        //这里出现问题时，会向进程发送信号，而一个进程的所有线程的pid都是一样的
        *p = 1;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    int n = pthread_create(&tid, nullptr, routine, (void*)"thread1");

    while(true)
    {
        char buffer[64];
        snprintf(buffer, sizeof buffer, "0x%x", tid);
        cout << "我是主线程！我创建的新线程的tid：" << buffer << " val: " << val << " &val: " << &val << endl;
        sleep(1);
    }
    return 0;
}